import hloading from '@/components/hloading.vue';
import { createVNode, render, ref, onMounted, onBeforeMount } from 'vue'
import { isEmpty, isFunction } from '@/plugins/util';


// 只有 loadingCount = 0 的时候才需要新建
// 只有 loadingCount = 1 的时候才需要关闭
let loadingCount = 0;

let containerEle = null;
export const useLoading = () => {

    const showLoading = () => {
        if (loadingCount <= 0 || containerEle == null) {
            containerEle = document.createElement('div');
            containerEle.setAttribute('name', 'hloading')
            document.body.append(containerEle);
            const vnode = createVNode(hloading, {
            })
            render(vnode, containerEle);
        }
        loadingCount += 1;
    }

    const closeLoading = () => {
        if (containerEle == null) {
            loadingCount = 0;
            return;
        }
        if (loadingCount <= 1) {
            //延迟500ms，防止网速特快加载中画面一闪而过
            if (containerEle != null) {
                containerEle.remove();
            }
        }
        loadingCount -= 1;
    }

    return {
        showLoading,
        closeLoading
    }

}
